/**
 * @Title: MrOPlateAndEngineNumberFragment.java
 * @author zengshuai shi
 * @date 2015年10月23日 上午11:06:57
 */
package com.shanghaionstar.mro;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.network.http.ResponseHandler;
import com.onstar.cn.mag.PlateEngineNumEntity;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.GaodeType;
import com.shanghaionstar.mro.bean.restrictions.RestrictionsBean;
import com.shanghaionstar.utils.FinalConfigParams;
import com.shanghaionstar.utils.RequestUtil;

/**
 * @author zengshuai shi
 * @ClassName: MrOPlateAndEngineNumberFragment
 * @Description: TODO
 * @date 2015年10月23日 上午11:06:57
 */
public class MrOPlateAndEngineNumberFragment extends MrOBaseFragment {
    private View view;
    @FindViewById
    private Button mro_plate_engine_number_commit;
    private String type;
    @FindViewById
    private EditText et_plate_number, et_engine_number;
    @FindViewById
    private TextView tv_poi_text, tv_plate_number_error, tv_engine_number_error, tv_poi_title;
    private MrOActivity parentActivity;
    @FindViewById
    private LinearLayout ll_engine_number;

    /**
     * (non-Javadoc)
     * <p>Title: onCreate</p>
     * <p>Description: </p>
     *
     * @param savedInstanceState
     * @see android.support.v4.app.Fragment#onCreate(Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parentActivity = (MrOActivity) getActivity();
    }

    /**
     * (non-Javadoc)
     * <p>Title: onCreateView</p>
     * <p>Description: </p>
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     * @see android.support.v4.app.Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.mro_plate_engine_number, container, false);
            injectView(view);
        } else {
            super.onResumeView(view);
        }
        return view;
    }

    /**
     * (non-Javadoc)
     * <p>Title: onResume</p>
     * <p>Description: </p>
     *
     * @see android.support.v4.app.Fragment#onResume()
     */
    @Override
    public void onResume() {
        super.onResume();
        Bundle bundle = getArguments();
        String bean = "";
        if (!CommonUtil.isNull(bundle)) {
            type = bundle.getString("type");
            bean = bundle.getString("bean");
        }
//        String plateNum = PreferencesUtls.getInstance(parentActivity).getString(FinalConfigParams.LICENSE_PLATE_NUM(parentActivity));
//        String engineNum = PreferencesUtls.getInstance(parentActivity).getString(FinalConfigParams.ENGINE_NUM(parentActivity));
        String plateNum = bundle.getString(FinalConfigParams.LICENSE_PLATE_NUM);
        String engineNum = bundle.getString(FinalConfigParams.ENGINE_NUM);
        if (!CommonUtil.isNull(plateNum)) {
            et_plate_number.setText(plateNum);
        }
        if (!CommonUtil.isNull(engineNum)) {
            et_engine_number.setText(engineNum);
        }
        if (type.equals(GaodeType.Restrictions.value())) {
            ll_engine_number.setVisibility(View.GONE);
            tv_poi_title.setText(R.string.mro_plate_number);
        } else if (type.equals(GaodeType.Violation.value())) {
            tv_poi_title.setText(R.string.mro_plate_or_engine_number);
            et_engine_number.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        et_engine_number.setCursorVisible(true);
                        isShowTextView(tv_engine_number_error, false);
                    } else {
                        if (CommonUtil.isNull(et_engine_number.getText().toString().trim())) {
                            isShowTextView(tv_engine_number_error, true);
                            tv_engine_number_error.setText(getString(R.string.mro_engine_number_null));
                        } else {
                            if (!CommonUtil.checkInput(et_engine_number.getText().toString().trim())) {
                                isShowTextView(tv_engine_number_error, true);
                                tv_engine_number_error.setText(getString(R.string.mro_engine_number_error));
                            } else {
                                isShowTextView(tv_engine_number_error, false);
                            }
                        }
                    }
                }
            });
        }
        et_plate_number.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    et_plate_number.setCursorVisible(true);
                    isShowTextView(tv_plate_number_error, false);
                } else {
                    String plateNumber = convertPlateNumber((et_plate_number.getText().toString().trim()));
                    if (CommonUtil.isNull(plateNumber)) {
                        isShowTextView(tv_plate_number_error, true);
                        tv_plate_number_error.setText(getString(R.string.mro_plate_number_null));
                    } else {
                        if (!CommonUtil.isPlateNumber(plateNumber)) {
                            isShowTextView(tv_plate_number_error, true);
                            tv_plate_number_error.setText(getString(R.string.mro_plate_number_error));
                        } else {
                            et_plate_number.setText(plateNumber);
                            isShowTextView(tv_plate_number_error, false);
                        }
                    }
                }
            }
        });
        if (!CommonUtil.isNull(type)) {
            if (!CommonUtil.isNull(bean)) {
                if (bean.equals("restrictionsBean")) {
                    RestrictionsBean restrictionsBean = (RestrictionsBean) bundle.getSerializable(GaodeType.Violation.value());
//                    if (!CommonUtil.isNull(restrictionsBean)) {
//                        if (restrictionsBean.getCode() == 40003) {
//                            tv_poi_text.setText(getString(R.string.mro_plate_number_error2));
//                        }
//                    }
                } else if (bean.equals("violationBean")) {
                    String msg = bundle.getString("msg");
                    if (!CommonUtil.isNull(msg) && msg.equalsIgnoreCase("License plate number error.")) {
                        tv_poi_text.setText(getString(R.string.mro_plate_number_error2));
                    }
                }
            } else {
                tv_poi_text.setText(getString(R.string.mro_plate_number_and_engine_number));
            }
        }

    }

    @OnClick({R.id.mro_plate_engine_number_commit})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.mro_plate_engine_number_commit:
                String plateNumber = et_plate_number.getText().toString().trim();
                String engineNumber = et_engine_number.getText().toString().trim();
                if (!CommonUtil.isNull(plateNumber)) {
                    plateNumber = convertPlateNumber(plateNumber);
                    if (!CommonUtil.isPlateNumber(plateNumber)) {
                        isShowTextView(tv_plate_number_error, true);
                        tv_plate_number_error.setText(getString(R.string.mro_plate_number_error));
                    } else {
                        et_plate_number.setText(plateNumber);
                        isShowTextView(tv_plate_number_error, false);
                        if (type.equals(GaodeType.Violation.value())) {
                            if (!CommonUtil.isNull(engineNumber)) {
                                if (!CommonUtil.checkInput(et_engine_number.getText().toString().trim())) {
                                    isShowTextView(tv_engine_number_error, true);
                                    tv_engine_number_error.setText(getString(R.string.mro_engine_number_error));
                                } else {
                                    isShowTextView(tv_engine_number_error, false);
                                    PlateEngineNumEntity entity = new PlateEngineNumEntity();
                                    entity.setLicensePlate(plateNumber);
                                    entity.setEngineNumber(engineNumber);
                                    RequestUtil.getInstance().putVehicleInfo(entity, new ResponseHandler() {
                                        @Override
                                        public void onSuccess(String tag, String json) {

                                        }

                                        @Override
                                        public void onError(String tag, HttpException e) {

                                        }
                                    }, getActivity());
//								PreferencesUtls.getInstance(parentActivity).put(FinalConfigParams.LICENSE_PLATE_NUM(parentActivity), plateNumber);
//								PreferencesUtls.getInstance(parentActivity).put(FinalConfigParams.ENGINE_NUM(parentActivity), engineNumber);
                                    parentActivity.hideSoftInput();
                                    parentActivity.fragmentManager.popBackStack();
                                    parentActivity.reRequestGaode(type, entity);
                                }
                            } else {
                                isShowTextView(tv_engine_number_error, true);
                                tv_engine_number_error.setText(getString(R.string.mro_engine_number_null));
                            }
                        } else if (type.equals(GaodeType.Restrictions.value())) {
                            PlateEngineNumEntity entity = new PlateEngineNumEntity();
                            entity.setLicensePlate(plateNumber);
                            RequestUtil.getInstance().putVehicleInfo(entity, new ResponseHandler() {
                                @Override
                                public void onSuccess(String tag, String json) {

                                }

                                @Override
                                public void onError(String tag, HttpException e) {

                                }
                            }, getActivity());
//                            PreferencesUtls.getInstance(parentActivity).put(FinalConfigParams.LICENSE_PLATE_NUM(parentActivity), plateNumber);
                            parentActivity.hideSoftInput();
                            parentActivity.fragmentManager.popBackStack();
                            parentActivity.reRequestGaode(type, entity);
                        }
                    }

                } else {
                    isShowTextView(tv_plate_number_error, true);
                    tv_plate_number_error.setText(getString(R.string.mro_plate_number_null));
                }

                break;

            default:
                break;
        }
    }


    /**
     * @param @param textView
     * @param @param isShow    设定文件
     * @return void    返回类型
     * @throws
     * @Title: isShowTextView
     * @Description: 是否显示
     */
    private void isShowTextView(TextView textView, boolean isShow) {
        if (isShow) {
            textView.setVisibility(View.VISIBLE);
        } else {
            textView.setText("");
            textView.setVisibility(View.GONE);
        }
    }

    private String convertPlateNumber(String number) {
        return number.toUpperCase();
    }
}
